package com.ITArmy.service.impl;

import com.ITArmy.beans.User;
import com.ITArmy.common.ServerResponse;
import com.ITArmy.dao.ConfigsMapper;
import com.ITArmy.dao.UserMapper;
import com.ITArmy.service.ConfigService;
import com.ITArmy.service.OtherLoginService;
import com.ITArmy.util.GetUserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class OtherLoginServiceImpl implements OtherLoginService {
    GetUserSession getUserSession = new GetUserSession();
    @Autowired
    UserMapper userMapper;
    @Autowired
    ConfigService configService;
    @Autowired
    ConfigsMapper configsMapper;

    //GitHub登录
    @Override
    public ServerResponse gitLogin(HttpServletRequest request, User user) {
        try {
            //先查询一下是否用这个github账户登陆过
            User selectUser = userMapper.selectById(user.getuId());
            if (selectUser == null) {
                int resultCount = userMapper.insertSelective(user);
                if (resultCount == 0) {
                    return ServerResponse.createByErrorMesage("git注册失败...");
                }
                //将用户数据放到session中
                getUserSession.addUserSession(request, user);
                //将用户的主题放到session中
                HttpSession session = request.getSession();
                session.setAttribute("config",configsMapper.selectByPrimaryKey(selectUser.getCoId()));
                return ServerResponse.createBySuccess("git注册成功...", "0");
            }
            //将用户数据放到session中
            getUserSession.addUserSession(request, user);
            //将用户的主题放到session中
            HttpSession session = request.getSession();
            System.out.println("主题为:"+configsMapper.selectByPrimaryKey(selectUser.getCoId())+"===================================");
            session.setAttribute("config",configsMapper.selectByPrimaryKey(selectUser.getCoId()));
            return ServerResponse.createBySuccessMessage("登录成功...");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return ServerResponse.createByErrorMesage("git注册失败...");
        }
    }
}
